<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\common\model\platform;

use app\common\model\BaseModel;

class PlatformSetting extends BaseModel
{
    public $keys = array(
        'pay', "transfer"
    );

    /**
     * 字段类型或者格式转换
     * @var array
     */
    protected $type = [
        'value' => 'json'
    ];

    public function getValueByKey($key, $plaid, $appid = 0)
    {
        $setting = $this->where(['key' => $key, 'plaid' => $plaid, 'appid' => $appid])->find();
        if ($setting) {
            $value = $setting->value;
            return $value;
        } else {
            return [];
        }
    }

    public function setValueByKey($formData, $key, $plaid, $appid = 0)
    {
        $setting = $this->where(['key' => $key, 'plaid' => $plaid, 'appid' => $appid])->findOrEmpty();
        $data = array(
            'key'   => $key,
            'value' => $formData,
            'appid' => $appid,
            'plaid' => $plaid
        );
        return $setting->save($data);
    }
}
